﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using QuanLyBanHang.DAL;
using QuanLyBanHang_NewApp.Entity;


namespace QuanLyBanHang_NewApp.BUS
{
    class SanPhamBUS
    {
        SanPhamDAL spDal;
        public SanPhamBUS()
        {
            spDal = new SanPhamDAL();

        }
        public DataTable getAllSP()
        {
            return spDal.SelectSP("");
        }
        public DataTable getByName(string name)
        {
            return spDal.SelectSP(name);
        }
        //public bool themSP(string tensp, int soluong, int gia, string loai, string mota)
        //{
        //    return spDal.insertSP(tensp, soluong, gia, loai, mota);

        //}
        //public bool capnhatSP(int masp, string tensp, int soluong, int gia, string loai, string mota)
        //{
        //    return spDal.updateSP(masp, tensp, soluong, gia, loai, mota);
        //}

        public bool xoaSP(long masp)
        {
            return spDal.deleteSP(masp);
        }

        public bool themSP(SanPham sp)
        {
            string tensp = sp.getAttribute(1);
            int sl = int.Parse(sp.getAttribute(4));
            double gia = double.Parse(sp.getAttribute(3));
            string loai = sp.getAttribute(2);
            string mota = sp.getAttribute(5);

            return spDal.insertSP(tensp, sl, gia, loai, mota);
            
        }

        public bool capnhatSP(SanPham sp)
        {

            string tensp = sp.getAttribute(1);
            int sl = int.Parse(sp.getAttribute(4));
            double gia = double.Parse(sp.getAttribute(3));
            string loai = sp.getAttribute(2);
            string mota = sp.getAttribute(5);
            long masp = long.Parse(sp.getAttribute(0));

            return spDal.updateSP(masp, tensp, sl, gia, loai, mota);

        }

        public SanPham findByID(long masp)
        {
            DataTable dt;
            dt = spDal.selectByID(masp);
            if (dt == null) return null;

            DataRow dtRow = dt.Rows[0];
            SanPham sp = new SanPham(long.Parse(dtRow["MaSanPham"].ToString()), dtRow["TenSanPham"].ToString(), dtRow["LoaiSanPham"].ToString()
                , double.Parse(dtRow["Gia"].ToString()), int.Parse(dtRow["SoLuong"].ToString()), dtRow["MoTa"].ToString());
            return sp;
        }

    }
}
